//
//  LogInView.swift
//  WuZhen
//
//  Created by 邹炽明 on 2025/5/18.
//

import SwiftUI

struct LogInView: View {
    @State private var email = ""
    @State private var password = ""
    
    var body: some View {
        ZStack {
            backgroundImage
            registrationForm
        }
    }
    
    // MARK: - Subviews
    
    private var backgroundImage: some View {
        Image("IMG_8970")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .ignoresSafeArea()
    }
    
    private var registrationForm: some View {
        GeometryReader { geometry in
            VStack(alignment: .leading, spacing: 20) {
                headerSection
                formFields
            }
            .padding(20)
            .padding(.vertical, 20)
            .background(BlurredBackground())
            .cornerRadius(30)
            .position(x: geometry.size.width / 2, y: geometry.size.height - 200)
        }
    }
    
    private var headerSection: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("注册")
                .font(.largeTitle)
                .bold()
                .foregroundColor(.primary)
            
            Text("热烈欢迎您的加入🎉🎊🎈🪅🙌🏻🍻🍾🥳👏🏻")
                .font(.headline)
                .fontWeight(.bold)
                .foregroundColor(.primary.opacity(0.7))
        }
        .padding(.horizontal)
    }
    
    private var formFields: some View {
        VStack(spacing: 16) {
            CustomTextField(text: $email, placeholder: "您的邮箱地址@iCloud.com")
                .emailFieldStyle()
            
            CustomSecureField(text: $password, placeholder: "请输入您的密码......")
                .passwordFieldStyle()
            
            registerButton
            
            termsAndPolicy
            
            Divider()
            
            loginPrompt
        }
    }
    
    private var registerButton: some View {
        Button(action: registerAction) {
            Text("注册一个新的账号")
                .fontWeight(.semibold)
                .frame(maxWidth: .infinity)
                .padding()
                .background(BlurredBackground())
                .clipShape(RoundedRectangle(cornerRadius: 10))
        }
    }
    
    private var termsAndPolicy: some View {
        Text("通过点击注册，您同意我们的 **[服务条款](http://www.wuzhen.com.cn)** 和 **隐私政策** 。")
            .font(.footnote)
            .fontWeight(.bold)
            .foregroundColor(.primary.opacity(0.7))
            .accentColor(.primary.opacity(0.7))
    }
    
    private var loginPrompt: some View {
        Text("已经有账户了吗? **登录**")
            .font(.footnote)
            .fontWeight(.bold)
            .foregroundColor(.primary.opacity(0.7))
            .accentColor(.primary.opacity(0.7))
    }
    
    // MARK: - Actions
    
    private func registerAction() {
        // Handle registration logic here
    }
}

// MARK: - Custom Text Fields

struct CustomTextField: View {
    @Binding var text: String
    let placeholder: String
    
    var body: some View {
        ZStack(alignment: .leading) {
            if text.isEmpty {
                Text(placeholder)
                    .foregroundColor(.primary.opacity(0.5))
            }
            TextField("", text: $text)
        }
        .padding()
        .background(BlurredBackground())
        .clipShape(RoundedRectangle(cornerRadius: 10))
    }
}

struct CustomSecureField: View {
    @Binding var text: String
    let placeholder: String
    
    var body: some View {
        ZStack(alignment: .leading) {
            if text.isEmpty {
                Text(placeholder)
                    .foregroundColor(.primary.opacity(0.5))
            }
            SecureField("", text: $text)
        }
        .padding()
        .background(BlurredBackground())
        .clipShape(RoundedRectangle(cornerRadius: 10))
    }
}

// MARK: - View Modifiers

extension View {
    func emailFieldStyle() -> some View {
        self
            .fontWeight(.bold)
            .textContentType(.emailAddress)
            .keyboardType(.emailAddress)
            .autocapitalization(.none)
            .disableAutocorrection(true)
    }
    
    func passwordFieldStyle() -> some View {
        self
            .fontWeight(.bold)
            .textContentType(.password)
    }
}

// MARK: - Background Views

struct BlurredBackground: View {
    var body: some View {
        VisualEffectView(effect: UIBlurEffect(style: .regular))
    }
}

struct VisualEffectView: UIViewRepresentable {
    var effect: UIVisualEffect?
    
    func makeUIView(context: Context) -> UIVisualEffectView {
        UIVisualEffectView()
    }
    
    func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
        uiView.effect = effect
    }
}

// MARK: - Preview

struct LogInView_Previews: PreviewProvider {
    static var previews: some View {
        LogInView()
    }
}